<?php
/**
 * 组织架构
 * @author eddietian
 * **/
apf_require_controller('Oa_Attendance_Abstract');
apf_require_class('Bll_Attendance_UserPositionInfo');

class Oa_Attendance_PositionController extends Oa_Attendance_AbstractController {

    public function handle_request() {
        $data = array();

        $root_position_id = APF::get_instance()->get_config("root_position_id");
        $bll_position = new Bll_Attendance_UserPositionInfo();
        $data = $bll_position->find_tree_by_position($root_position_id);

        $data = $this->format_tree($data);

        $this->request->set_attribute('title',"岗位树形结构");
        $this->request->set_attribute('data',$data);
        return 'Oa_Attendance_Position';
    }

    public function format_tree($data) {
        $s = "<li>";
        foreach($data as $v) {
            $s .= "<span>{$v['position_name']}&nbsp;{$this->get_arr_name($v['user_name'])}</span>";
            $s .= $this->get_child_tree($v['children']);
        }
        $s .= "</li>";
        return $s;
    }

    private function get_child_tree($arr) {
        if(empty($arr)){return "";}
        $s = "";
        foreach($arr as $v) {
            $s .= "<li><span>{$v['position_name']}&nbsp;{$this->get_arr_name($v['user_name'])}{$this->get_status($v['status'])}</span>";
            $s .= "{$this->get_child_tree($v['children'])}";
            $s .= "</li>";
        }
        return "<ul>".$s."</ul>";
    }

    private function get_status($s) {
        if($s == '2') {
            return "&nbsp;<i>兼职</i>";
        }
        return "";
    }

    private function get_arr_name($arr) {
        if(empty($arr)) {return "";}
        $s = "";
        foreach($arr as $key => $v) {
            if($key>0) {
                $s .= "、";
            }
            $s .= '['.$v.']';
        }
        return $s;
    }
}
